/**
 * 产品类别故障维护
 * @author mengyb
 * @date 2019-04-17
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        layui = win.layui,
        table,
        table_where = {},
        element,
        vueData = {},
        cols = [[
            {type:'checkbox',title:'选择',width:60,fixed:'left'},
            {field:'ROW_ID',title:'序号',width:60,fixed:'left'},
            {field:'MMRM001',title:'产品分类',width:150,fixed:'left',sort:true,search:'default',searchOpts:{icon:true}},
            {field:'MMRM002',title:'故障编号',width:80,sort:true,search:'default',searchOpts:{icon:true}},
            {field:'MMRLL003',title:'说明',width:80,sort:true,search:'default'},
            {field:'MMRLL004',title:'助记码',width:80,sort:true,search:'default'},
            {field:'MMRMACTI',title:'有效',width:80,filterDatas:app.datas_('sys_y_n'),sort:true},
            {title:'',width:60,toolbar:'#script-gwai670setting',fixed:'right',align:'center',unresize:true}

        ]],

        gwai670SettingAction,
        moduleName = "产品类别故障",
        editArea = ['400px', '380px'];


    var Gwai670 = app.Module.extend({

        init: function () {
            self=this;
            self._super({
                _tag: 'gwai670',
                _searchs:[
                    {id:'gwai670',tableId:'gwai670_table',datas:cols[0]}
                ],
                _loadTplsFull:[
                    {dom:'gwai670_edit',tpl:'base/gwai670_edit'},
                    {dom:'common_open_window',tpl:'common/_open_window'}
                ]
            }, win);
        },

        initTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                table.render({
                    elem: app.getByid(doc, 'gwai670_table'),
                    url: g_ctxPath + 'base/gwai670/getList',
                    method: 'post',
                    id: 'gwai670_table',
                    title: moduleName+'表',
                    size: _layuiConfig.ui_size,
                    autoSort: false, //禁用前端自动排序
                    toolbar: '#table-toolbar',
                    defaultToolbar: ['filter', 'exports'],
                    height: 'full',
                    cols: cols,
                    loading: true,//是否显示加载条
                    page: true,
                    where: table_where,
                    done: function (res, curr, count) {

                    }
                });

                layui.laytpl(app.getByid(doc, 'script-gwai670setting-action').innerHTML).render({}, function (string) {
                    gwai670SettingAction = string;
                });

                //弹出层索引
                var tipsIndex=null;


                //监听行工具栏事件，弹出编辑与删除按钮 lay-filter指定
                table.on('tool(gwai670_table)', function (obj) {
                    layer.tips(gwai670SettingAction, obj.td, {
                        tips: [3, '']
                        , time: 3000
                        , anim: 500
                        , maxWidth: 120
                        , skin: 'layui-table-tips'
                        , success: function (layero, index) {
                            tipsIndex = index;
                            layero.find('li').on('click', function () {
                                switch ($(this).attr('inx')) {
                                    case '1':
                                        self._openTplFull({
                                            tag: 'gwai670_edit',
                                            tagData: {
                                                isAdd:false,
                                                obj : obj,
                                                moduleName : moduleName
                                            },
                                            index: 0,
                                            openOpts: {
                                                title: '修改'+moduleName+'信息',
                                                area:editArea
                                            }
                                        });
                                        break;
                                    case '2':
                                        self.delRow(obj);
                                        break;
                                }
                                layer.close(index);
                                tipsIndex = null;
                            });
                        }
                    });
                    return false;

                });

                //监听表头工具栏事件
                table.on('toolbar(gwai670_table)', function (obj) {
                    var checkStatus = table.checkStatus('gwai670_table');
                    switch (obj.event) {
                        case 'add':
                            self._openTplFull({
                                tag: 'gwai670_edit',
                                tagData: {
                                    isAdd: true,
                                    moduleName:moduleName
                                },
                                index: 0,
                                openOpts: {
                                    title: '新增'+moduleName+'信息',
                                    area:editArea
                                }
                            })
                            break;
                        case 'del':
                            self.delRows(checkStatus);
                            break;
                    }

                });

                //关闭弹出层
                $(doc).on('click', function () {
                    tipsIndex && layer.close(tipsIndex);
                });

                //监听行双击事件
                table.on('rowDouble(gwai670_table)',function (obj) {
                    self._openTplFull({
                        tag:'gwai670_edit',
                        tagData: {
                            obj:obj,
                            isAdd:false,
                            moduleName:moduleName,
                            is_detail: true
                        },
                        index:0,
                        openOpts:{
                            title: '修改'+moduleName+'信息',
                            area:editArea
                        }
                    })
                })

            });
        },

        reloadTableCurr: function () {
            self._reloadTable({id: 'gwai670_table', where: table_where});
        },

        delRow:function (obj) {
            app.confirm({
                msg:'确认删除该条信息吗？',
                ok: function () {
                    app.ajax({
                        xurl:'base/gwai670/delRow',
                        xdata:obj.data,
                        loading:true,
                        okmsg:moduleName+'信息删除成功',
                        cb: function (result) {
                            if (result.ok) {
                                self.reloadTableCurr();
                            }
                        }
                    })

                }
            })
        },

        delRows:function (checkStatus) {
            if(checkStatus.data.length>0){
                var _xdata = [];
                checkStatus.data.forEach(function (value) {
                    var _temp={};
                    _temp.MMRMENT=value.MMRMENT;
                    _temp.MMRM001=value.MMRM001;
                    _temp.MMRM002=value.MMRM002;
                    _xdata.push(_temp);
                });
                app.confirm({
                    msg:'确认删除所选择的信息吗？',
                    ok: function () {
                        app.ajax({
                            xurl:'base/gwai670/delRows',
                            xdata:_xdata,
                            toJsonStr:true,
                            okmsg:moduleName+'信息删除成功',
                            loading:true,
                            cb: function (result) {
                                if (result.ok) {
                                    self.reloadTableCurr();
                                }
                            }
                        })

                    }
                })

            }else{

                app.showMessage("请先选择待删除的信息！")
            }
        },

        _ready: function () {
            vueData.serchData=table_where;
            this._super({
               vue:[{
                   el: '#page',
                   id: 'page',
                   data: vueData,
                   searchId: 'gwai670',
                   methods:{
                       handleIconClick: function (field) {
                           var vue = this;
                           if (field == 'MMRM001') {//产品分类开窗
                               self._openTplFull({
                                   tag: 'common_open_window',
                                   tagData: {
                                       config: {
                                           method:'q_gwah001',
                                           columnList: [
                                               {label: '产品分类编号', prop: 'GWAH001', width: '150'},
                                               {label: '说明', prop: 'GWAHL003', width: '150'},
                                               {label: '助记码', prop: 'GWAHL004', width: '150'}
                                           ],
                                       },
                                       cb: function (data) {
                                           vue.isearch.fields.MMRM001 = data.GWAH001;
                                       }
                                   },
                                   index: 1,
                                   openOpts: {
                                       title: '选择产品分类',
                                       area: ['600px', '430px']
                                   }
                               });
                           }else if(field =='MMRM002'){//故障开窗
                               self._openTplFull({
                                   tag: 'common_open_window',
                                   tagData: {
                                       config: {
                                           method:'q_mmrl001',
                                           columnList: [
                                               {label: '故障编号', prop: 'MMRL001', width: '150'},
                                               {label: '说明', prop: 'MMRLL003', width: '150'},
                                               {label: '助记码', prop: 'MMRLL004', width: '150'}
                                           ]
                                       },
                                       cb: function (data) {
                                           vue.isearch.fields.MMRM002 = data.MMRL001;
                                       }
                                   },
                                   index: 1,
                                   openOpts: {
                                       title: '选择故障',
                                       area: ['600px', '465px']
                                   }
                               });
                           }
                       }
                   }
               }]

            });
            self.initTable();
        },

        _lazy: function () {
            this._super();
            layui.use(['table'], function () {
                element=layui.element;
            });
        }



    });

    return new Gwai670();


});
